package com.sunwayworld.basemodule.basedata.sampletype.service.impl;

import com.sunwayworld.basemodule.samplemanage.sample.bean.LimsSampleBean;
import com.sunwayworld.basemodule.samplemanage.sample.service.LimsSampleService;
import com.sunwayworld.framework.exception.InvalidDataException;
import com.sunwayworld.framework.i18n.I18nHelper;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.basedata.sampletype.dao.LimsSampleTypeDao;
import com.sunwayworld.basemodule.basedata.sampletype.bean.LimsSampleTypeBean;
import com.sunwayworld.basemodule.basedata.sampletype.service.LimsSampleTypeService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsSampleTypeServiceImpl implements LimsSampleTypeService {

    @Autowired
    private LimsSampleTypeDao limsSampleTypeDao;

    @Autowired
    private LimsSampleService sampleService;

    @Override
    public LimsSampleTypeDao getDao() {
        return limsSampleTypeDao;
    }

    @Override
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsSampleTypeBean sampleType = jsonWrapper.parseUnique(LimsSampleTypeBean.class);
        sampleType.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(sampleType);
        return sampleType.getId();
    }

    @Override
    @Transactional
    public void delete(RestJsonWrapperBean wrapper) {
        List<Long> sampleTypeIds = wrapper.parseId(Long.class);
        List<LimsSampleTypeBean> sampleTypeBeans = getDao().selectListByIds(sampleTypeIds);
        if (sampleTypeBeans.isEmpty()) return;
        List<String> sampleTypeCodes = sampleTypeBeans.stream().map(LimsSampleTypeBean::getSampleTypeCode).collect(Collectors.toList());
        List<LimsSampleBean> sampleBeans = sampleService.selectListByFilter(SearchFilter.instance().match("sampleTypeCode", sampleTypeCodes).filter(MatchPattern.OR));
        // 校验是否存在被引用的样本类型
        if (!sampleBeans.isEmpty()){
            String msg = sampleBeans.stream().map(LimsSampleBean::getSampleTypeCode).distinct().collect(Collectors.joining(","));
            throw new InvalidDataException(I18nHelper.getMessage("EXCEPTION.TIP.SAMPLETYPE.DELETE").concat("【").concat(msg).concat("】").concat(I18nHelper.getMessage("EXCEPTION.TIP.FORBIDDEN.DELETE")));
        }
        getDao().deleteByIdList(sampleTypeIds);
    }
}
